<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--搜索区域-->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入名称" clearable style="width: 240px" @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="头衔" prop="level">
          <el-select v-model="queryParams.level" placeholder="头衔" clearable style="width: 240px">
            <el-option key="1" label="高级" value="1"/>
            <el-option key="2" label="首席" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
                          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!--操作按钮区域-->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['course:subject:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['course:trainer:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['course:trainer:remove']">删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>
      <!--数据列表区域-->
      <el-table v-loading="loading" :data="trainerList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="编号" align="center" key="id" prop="id" v-if="columns[0].visible" />
        <el-table-column label="名称" align="center" key="name" prop="name" v-if="columns[1].visible" :show-overflow-tooltip="true" />
        <el-table-column label="头像" align="center" key="avatar" prop="avatar" v-if="columns[2].visible" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <img :src="scope.row.avatar" width="100" />
          </template>
        </el-table-column>
        <el-table-column label="头衔" align="center" key="level" v-if="columns[3].visible">
          <template slot-scope="scope">
            <span v-if="scope.row.level == 1">高级</span>
            <span v-if="scope.row.level == 2">首席</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[4].visible" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['course:trainer:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['course:trainer:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页栏-->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.currentPage"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"/>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入培训师名称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="头衔" prop="level">
              <el-select v-model="form.level" placeholder="请选择头衔">
                <el-option :key="1" label="高级" :value="1"></el-option>
                <el-option :key="2" label="首席" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="资历">
          <el-input v-model="form.career" type="textarea" placeholder="请输入培训师资历" />
        </el-form-item>
        <el-form-item label="简介" prop="email">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入培训师简介"/>
        </el-form-item>
        <el-form-item label="头像">
          <ImageUpload placeholder="上传头像" ref="imageUpload" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listTrainer, addTrainer, getTrainer, updateTrainer} from "@/api/course/trainer";

export default {
  // 数据模型
  data(){
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      trainerList: null,
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        name: undefined,
        level: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `编号`, visible: true },
        { key: 1, label: `名称`, visible: true },
        { key: 2, label: `头像`, visible: true },
        { key: 3, label: `头衔`, visible: true },
        { key: 4, label: `创建时间`, visible: true }
      ],
      // 表单参数
      form: {},
      // 弹出层标题
      title: "",
      // 表单校验
      rules: {
        name: [
          { required: true, message: "培训师名称不能为空", trigger: "blur" },
          { min: 2, max: 8, message: '培训师名称长度必须介于 2 和 8 之间', trigger: 'blur' }
        ],
        level: [
          { required: true, message: "培训师头衔不能为空", trigger: "blur" },
        ],
      }
    }
  },
  // 钩子函数
  created(){
    this.getList();
  },
  // 自定义函数 (hehe)
  methods:{
    // 搜索
    handleQuery(){
      this.queryParams.currentPage = 1;
      this.getList();
    },
    // 重置搜索条件
    resetQuery(){
      this.dateRange = [];
      this.queryParams = {
        currentPage: 1,
        pageSize: 10,
        name: undefined,
        level: undefined
      };
      this.getList();
    },
    // 打开新增窗口
    handleAdd(){
      this.reset();
      this.open = true;
      this.title = "新增培训师";
    },
    // 打开修改窗口
    handleUpdate(row){
      this.reset();
      const id = row.id || this.ids;
      // 根据ID查询数据
      getTrainer(id).then(response => {
        // 把trainer数据赋值给form对象
        this.form = response.data
        // 显示用户已经上传的图片
        this.$nextTick( () => {
          this.$refs.imageUpload.fileList.push({url: this.form.avatar});
        })
        // 显示对话框
        this.open = true
        // 修改对话框的标题
        this.title = "修改培训师"
      })
    },
    // 删除
    handleDelete(){

    },
    // 查询数据
    getList(){
      this.loading = true;
      listTrainer(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.trainerList = response.data.data;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 复选框
    handleSelectionChange(selection){
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 重置表单
    reset(){
      this.form = {
        id: undefined,
        name: undefined,
        level: undefined,
        career: undefined,
        intro: undefined,
        avatar: undefined
      }
      // 重置上传图片数据
      if( this.$refs.imageUpload != undefined){
        this.$refs.imageUpload.fileList = [];
      }
      // 重置提示信息等
      this.resetForm("form");
    },
    // 表单提交按钮
    submitForm(){
      // 将上传的图片地址设置到form.avatar属性中
      this.form.avatar = this.$refs.imageUpload.fileList[0].url;
      // 表单校验通过才需要提交表单，不通过，给出提示即可
      this.$refs["form"].validate(valid => {
        // 表示表单校验通过
        if(valid){
          // 判断是新增的操作还是修改的操作
          if(this.form.id == undefined){// 新增操作
            addTrainer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            })
          }else{// 修改操作
            updateTrainer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          }
        }
      })
    },
    // 表单取消按钮
    cancel(){
      this.open = false;
      this.reset();
    }
  }
}
</script>
